package com.exam.service.impl;

import com.exam.mapper.*;
import com.exam.pojo.ComposePaper;
import com.exam.pojo.PaperManage;
import com.exam.pojo.PaperMap;
import com.exam.service.ChoiceQuestionService;
import com.exam.service.ComposePaperService;
import com.exam.service.FillQuestionService;
import com.exam.service.JudgeQuestionService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ComposePaperServiceImpl implements ComposePaperService {
    @Resource
    private PaperMapMapper paperMapMapper;
    @Resource
    private ChoiceQuestionMapper choiceQuestionMapper;
    @Resource
    private FillQuestionMapper fillQuestionMapper;
    @Resource
    private JudgeQuestionMapper judgeQuestionMapper;

    @Override
    @Transactional
    public void composing(ComposePaper composePaper) {
        if ("admin".equals(composePaper.getQuestionCreatorId())){
            //选择题
            List<Integer> choices = choiceQuestionMapper.queryIdBySubject(composePaper.getChoiceSubject(), composePaper.getChoiceNumber());
            if (choices == null) throw new RuntimeException("选择题数据库获取失败");
            for (Integer choice : choices) {
                int insert = paperMapMapper.insert(new PaperMap(composePaper.getPaperId(), 1, choice));
                if (insert==0) throw new RuntimeException("选择题组卷失败");
            }
            //填空题
            List<Integer> fills = fillQuestionMapper.queryIdBySubject(composePaper.getFillSubject(), composePaper.getFillNumber());
            if (fills == null) throw new RuntimeException("填空题数据库获取失败");
            for (Integer fill : fills) {
                int insert = paperMapMapper.insert(new PaperMap(composePaper.getPaperId(), 2, fill));
                if (insert == 0) throw new RuntimeException("填空题组卷失败");
            }
            //判断题
            List<Integer> judges = judgeQuestionMapper.queryIdBySubject(composePaper.getJudgeSubject(), composePaper.getJudgeNumber());
            if (judges == null) throw new RuntimeException("判断题数据库获取失败");
            for (Integer judge : judges) {
                int insert = paperMapMapper.insert(new PaperMap(composePaper.getPaperId(), 3, judge));
                if (insert == 0) throw new RuntimeException("判断题组卷失败");
            }
        }else {
            //选择题
            List<Integer> choices = choiceQuestionMapper.queryIdBySubjectAndQuestionCreatorId(
                    composePaper.getChoiceSubject(),
                    composePaper.getChoiceNumber(),
                    composePaper.getQuestionCreatorId());
            if (choices == null) throw new RuntimeException("选择题数据库获取失败");
            for (Integer choice : choices) {
                int insert = paperMapMapper.insert(new PaperMap(composePaper.getPaperId(), 1, choice));
                if (insert==0) throw new RuntimeException("选择题组卷失败");
            }
            //填空题
            List<Integer> fills = fillQuestionMapper.queryIdBySubjectAndQuestionCreatorId(
                    composePaper.getFillSubject(),
                    composePaper.getFillNumber(),
                    composePaper.getQuestionCreatorId());
            if (fills == null) throw new RuntimeException("填空题数据库获取失败");
            for (Integer fill : fills) {
                int insert = paperMapMapper.insert(new PaperMap(composePaper.getPaperId(), 2, fill));
                if (insert == 0) throw new RuntimeException("填空题组卷失败");
            }
            //判断题
            List<Integer> judges = judgeQuestionMapper.queryIdBySubjectAndQuestionCreatorId(
                    composePaper.getJudgeSubject(),
                    composePaper.getJudgeNumber(),
                    composePaper.getQuestionCreatorId());
            if (judges == null) throw new RuntimeException("判断题数据库获取失败");
            for (Integer judge : judges) {
                int insert = paperMapMapper.insert(new PaperMap(composePaper.getPaperId(), 3, judge));
                if (insert == 0) throw new RuntimeException("判断题组卷失败");
            }
        }
    }
}
